package com.th.supcom.pds.outdrugreturnapply.client.widget;

import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.constant.KeysConstants;

/**
 * 门诊退药时间监听
 * 
 * @author yzhang
 */
public class OutDrugReturnApplyListener
{
    private OutDrugReturnApplyFields m_fields;
    private OutDrugReturnApplyButtons m_buttons;
    private OutDrugReturnApplyAction m_action;
    private OutDrugReturnApplyMasterGridPanel m_masterGridPanel;
    /** 需要监听的全局面板. */
    private Component m_keyNavComponent;

    /**
     * 初始化所有的Listener
     */

    public void initListeners ()
    {
        addButtonListener ();
        addGridListener ();
        addEnterKeyListener ();
    }

    public OutDrugReturnApplyListener ()
    {
        super ();
    }

    /**
     * 界面按钮监听事件
     */
    private void addButtonListener ()
    {
        m_buttons.getComfirmBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_action.doDrugReturn ();
            }
        });
        m_buttons.getQueryBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                Map <String, Object> conditionMap = new HashMap <String, Object> ();
                conditionMap.put ("patientId", m_fields.getPatiIdField ().getValue ());
                conditionMap.put ("prescNo", m_fields.getPrescNoField ().getValue ());
                conditionMap.put ("beginDate", m_fields.getBeginField ().getValue ());
                conditionMap.put ("endDate", m_fields.getEndField ().getValue ());
                m_action.findDrugReturnInfo (conditionMap);
            }
        });
        m_buttons.getClearBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_action.clearData ();
            }
        });
        m_fields.getPatiIdField ().addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                if (event.getKeyCode () == KeysConstants.VK_ENTER)
                {
                    Map <String, Object> conditionMap = new HashMap <String, Object> ();
                    conditionMap.put ("patientId", m_fields.getPatiIdField ().getValue ());
                    conditionMap.put ("prescNo", m_fields.getPrescNoField ().getValue ());
                    conditionMap.put ("beginDate", m_fields.getBeginField ().getValue ());
                    conditionMap.put ("endDate", m_fields.getEndField ().getValue ());
                    m_action.findDrugReturnInfo (conditionMap);
                }
            }
        });
        m_fields.getPrescNoField ().addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                if (event.getKeyCode () == KeysConstants.VK_ENTER)
                {
                    Map <String, Object> conditionMap = new HashMap <String, Object> ();
                    conditionMap.put ("patientId", m_fields.getPatiIdField ().getValue ());
                    conditionMap.put ("prescNo", m_fields.getPrescNoField ().getValue ());
                    conditionMap.put ("beginDate", m_fields.getBeginField ().getValue ());
                    conditionMap.put ("endDate", m_fields.getEndField ().getValue ());
                    m_action.findDrugReturnInfo (conditionMap);
                }
            }
        });
    }

    public void addGridListener ()
    {
        m_masterGridPanel.getCheckmodel ().addSelectionChangedListener (new SelectionChangedListener <PdsSupplyModel> ()
        {
            @Override
            public void selectionChanged (SelectionChangedEvent <PdsSupplyModel> se)
            {
                if (se == null)
                {
                    return;
                }
                PdsSupplyModel model = se.getSelectedItem ();
                m_action.doSelectedChange (model);
            }
        });
    }

    /**
     * 监听快捷键
     */
    private void addEnterKeyListener ()
    {
        new KeyNav <ComponentEvent> (m_keyNavComponent)
        {
            @Override
            public void onKeyPress (final ComponentEvent ce)
            {
                final int keyCode = ce.getKeyCode ();

                if (ce.isAltKey ())
                {
                    executAltEvent (keyCode);
                }

            }
        };
    }

    /**
     * 执行Alt+?快捷键
     */
    private void executAltEvent (final int keyCode)
    {
        if (keyCode == KeysConstants.VK_Q)
        {
            Map <String, Object> conditionMap = new HashMap <String, Object> ();
            conditionMap.put ("patientId", m_fields.getPatiIdField ().getValue ());
            conditionMap.put ("prescNo", m_fields.getPrescNoField ().getValue ());
            conditionMap.put ("beginDate", m_fields.getBeginField ().getValue ());
            conditionMap.put ("endDate", m_fields.getEndField ().getValue ());
            m_action.findDrugReturnInfo (conditionMap);
            return;
        }

        if (keyCode == KeysConstants.VK_O)
        {
            m_action.doDrugReturn ();
            return;
        }
        if (keyCode == KeysConstants.VK_R)
        {
            m_action.clearData ();
            return;
        }
    }

    /**
     * 设置 文本域
     * 
     * @param fields
     */
    public void setFields (OutDrugReturnApplyFields fields)
    {
        m_fields = fields;
    }

    public void setMasterGridPanel (OutDrugReturnApplyMasterGridPanel masterGridPanel)
    {
        m_masterGridPanel = masterGridPanel;
    }

    /**
     * 设置 按鈕
     * 
     * @param buttons
     */
    public void setButtons (OutDrugReturnApplyButtons buttons)
    {
        m_buttons = buttons;
    }

    /**
     * 设置 退药action
     * 
     * @param action
     */
    public void setAction (OutDrugReturnApplyAction action)
    {
        m_action = action;
    }

    /**
     * 设置 快捷键要监听的全局面板
     * 
     * @param keyNavComponent
     */
    public void setKeyNavComponent (Component keyNavComponent)
    {
        m_keyNavComponent = keyNavComponent;
    }

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
